var app = require("lzbjpk_app");

/**
 * 类构造
 */
var BJPKRoomMgr = app.BaseClass.extend({

	/**
	 * 初始化
	 */
	Init: function () {

		this.JS_Name = "BJPKRoomMgr";

		this.ComTool = app[app.subGameName + "_ComTool"]();
		this.ShareDefine = app[app.subGameName + "_ShareDefine"]();
		this.NetManager = app[app.subGameName + "_NetManager"]();
		this.FormManager = app[app.subGameName + "_FormManager"]();
		this.SysNotifyManager = app[app.subGameName + "_SysNotifyManager"]();
		this.LocalDataManager = app.LocalDataManager();
		this.BJPKRoom = app[app.subGameName.toUpperCase() + "Room"]();
		this.BJPKRoomSet = app[app.subGameName.toUpperCase() + "RoomSet"]();

		this.NetManager.RegNetPack(app.subGameName + ".C" + app.subGameName.toUpperCase() + "CreateRoom", this.OnPack_CreateRoom, this);
		this.NetManager.RegNetPack(app.subGameName + ".C" + app.subGameName.toUpperCase() + "GetRoomInfo", this.OnPack_GetRoomInfo, this);

		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_CardReadyChg", this.OnPack_CardReadyChg, this);
		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_SetStart", this.OnPack_SetStart, this);
		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_Abandon", this.OnPack_PosQiPai, this);
		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_CardReadyChg", this.OnPack_PosCardReady, this);
		//玩家拿牌
		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_Ranked", this.OnPack_Ranked, this);
		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_SetEnd", this.OnPack_SetEnd, this);
		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_StartRound", this.OnPack_StartRound, this);
		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_PosOpCard", this.OnPack_PosOpCard, this);
		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_RoomEnd", this.OnPack_RoomEnd, this);
		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_RoomStart", this.OnPack_RoomStart, this);
		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_Chatmessage", this.OnPack_ChatMessage, this);

		//比赛分通知
		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_SportsPointNotEnough", this.OnPack_SportsPointNotEnough, this);
		this.NetManager.RegNetPack("S" + app.subGameName.toUpperCase() + "_SportsPointEnough", this.OnPack_SportsPointEnough, this);
		this.NetManager.RegNetPack("SRoom_SportsPointChange", this.OnPack_SportsPointChange, this);

		this.HeroManager = app[app.subGameName + "_HeroManager"]();

		this.OnReload();

		console.log("Init");

	},

	/**
	 * 重登
	 */
	OnReload: function () {
		this.enterRoomID = 0;
		//获取启动客户端进入的房间KEY
		this.loginEnterRoomKey = 0;

		this.BJPKRoom.OnReload();
	},

	OnSwithSceneEnd: function (sceneType) {

		//如果退出房间场景了,清除数据
		if (sceneType != "fightScene") {
			this.OnReload();
		}
	},
	CheckOnEvent: function (serverPack) {
		if (serverPack["roomID"] == this.enterRoomID) {
			return true;
		}
		return false;
	},
	//----------------------收包接口-----------------------------


	//创建房间完成
	OnPack_CreateRoom: function (serverPack) {
		console.log('=========LZBJPKRoomMgr OnPack_CreateRoom============')
		let agrs = Object.keys(serverPack);
		if (0 == agrs.length)//俱乐部会回空包
			return;
		if (serverPack.createType == 2) {
			app[app.subGameName + "_FormManager"]().ShowForm('UIDaiKai');
			app[app.subGameName + "_FormManager"]().CloseForm('UICreatRoom');
		} else {
			this.SendGetRoomInfo(serverPack.roomID);
		}
	},


	//获取到房间完整信息
	OnPack_GetRoomInfo: function (serverPack) {
		console.log('===============LZBJPKRoomMgr OnPack_GetRoomInfo==============')
		if (serverPack["NotFind_Player"]) {
			app[app.subGameName + "_SysNotifyManager"]().ShowSysMsg('PLAYER_NOT_ROOM');
			app[app.subGameName + "Client"].ExitGame();
			return;
		}
		else if (serverPack["NotFind_Room"]) {
			app[app.subGameName + "_SysNotifyManager"]().ShowSysMsg('Room_NotFindRoom');
			app[app.subGameName + "Client"].ExitGame();
			return;
		}
		this.enterRoomID = serverPack["roomID"];
		this.BJPKRoom.OnReload();
		this.clubId = serverPack["cfg"]["clubId"];
		this.unionId = serverPack["cfg"]["unionId"];
		this.BJPKRoom.OnInitRoomData(serverPack);
		//进入打牌场景
		if (app[app.subGameName + "_SceneManager"]().GetSceneType() != app.subGameName + "Scene") {
			app[app.subGameName + "_SceneManager"]().LoadScene(app.subGameName + "Scene");
		} else {
			app[app.subGameName + "_FormManager"]().ShowForm("game/" + app.subGameName.toUpperCase() + "/ui/UI" + app.subGameName.toUpperCase() + "Play");
		}
	},
	//--------------notify-----------------
	//set开始
	OnPack_SetStart: function (serverPack) {
		console.log('=======LZBJPKRoomMgr RoomMgr OnPack_SetStart==========');
		let roomID = serverPack["roomID"];
		let setInfo = serverPack["setInfo"];
		this.BJPKRoom.OnSetStart(setInfo);
		let key = `${app.subGameName.toUpperCase()}_SetStart`;
		app[app.subGameName + "Client"].OnEvent(key, serverPack);
	},
	//弃牌
	OnPack_PosQiPai: function (serverPack) {
		let roomID = serverPack["roomID"];
		let pos = serverPack["posID"];
		let abandonFlag = serverPack["abandonFlag"];
		let key = `${app.subGameName.toUpperCase()}_PosQiPai`;
		app[app.subGameName + "Client"].OnEvent(key, serverPack);
	},
	//比牌中
	OnPack_PosCardReady: function (serverPack) {
		let roomID = serverPack["roomID"];
		let pos = serverPack["posId"];
		let isReady = serverPack["isReady"];
		let key = `${app.subGameName.toUpperCase()}_PosCardReady`;
		app[app.subGameName + "Client"].OnEvent(key, serverPack);
	},
	//
	OnPack_Ranked: function (serverPack) {
		let key = app.subGameName.toUpperCase() +"_Ranked";
		app[app.subGameName + "Client"].OnEvent(key, serverPack);
	},

	//小局结算
	OnPack_SetEnd: function (serverPack) {
		let roomID = serverPack["roomID"];
		let setState = serverPack["setState"];
		let sRankingResult = serverPack["sRankingResult"];
		let playerResults = sRankingResult["playerResults"];
		let pCard1 = sRankingResult["pCard1"];
		let pResult1 = sRankingResult["pResult1"];
		let pCard2 = sRankingResult["pCard2"];
		let pResult2 = sRankingResult["pResult2"];
		let pCard3 = sRankingResult["pCard3"];
		let pResult3 = sRankingResult["pResult3"];


		this.BJPKRoom.OnSetEnd(serverPack);
		let key = `${app.subGameName.toUpperCase()}_SetEnd`;
		app[app.subGameName + "Client"].OnEvent(key, serverPack);
	},

	//round开始
	OnPack_StartRound: function (serverPack) {
		if (this.CheckOnEvent(serverPack) == false) {
			return;
		}
		let roomID = serverPack["roomID"];
		let setRound = serverPack["room_SetWait"];
		this.BJPKRoom.GetRoomSet().OnStartRound(setRound);
		let key = `${app.subGameName.toUpperCase()}_StartRound`;
		app[app.subGameName + "Client"].OnEvent(key, serverPack);
	},
	//位置执行动作通知
	OnPack_PosOpCard: function (serverPack) {
		let roomID = serverPack["roomID"];
		this.BJPKRoom.UpdataInfo(serverPack);
		//存储上家的动作
		let pos = serverPack["pos"];
		let opType = serverPack["opType"];
		let value = serverPack["value"];
		let key = `${app.subGameName.toUpperCase()}_PosOpCard`;
		app[app.subGameName + "Client"].OnEvent(key, serverPack);
	},

	//房间开始
	OnPack_RoomStart: function (serverPack) {
		console.error("OnPack_RoomStart:", serverPack);
		app[app.subGameName + "Client"].OnEvent("RoomStart", {});
	},
	//局内消息表情
	OnPack_ChatMessage: function (serverPack) {
		app[app.subGameName + "Client"].OnEvent("ChatMessage", serverPack);
	},
	//比赛分不足时通知
	OnPack_SportsPointNotEnough: function (serverPack) {
		app[app.subGameName + "Client"].OnEvent("SportsPointNotEnough", serverPack);
	},
	OnPack_SportsPointEnough: function (serverPack) {
		app[app.subGameName + "Client"].OnEvent("SportsPointEnough", serverPack);
	},
	//玩家的比赛分在游戏外被改变通知
	OnPack_SportsPointChange: function (serverPack) {
		this[app.subGameName.toUpperCase() + "Room"].OnSportsPointChange(serverPack);
		app[app.subGameName + "Client"].OnEvent("RoomSportsPointChange", serverPack);
	},
	//房间结束
	OnPack_RoomEnd: function (serverPack) {
		this.BJPKRoom.OnRoomEnd(serverPack);
		let key = app.subGameName.toUpperCase() + "_RoomEnd";
		app[app.subGameName + "Client"].OnEvent(key, serverPack);
	},

	OnPack_CardReadyChg: function (serverPack) {
		let key = app.subGameName.toUpperCase() + "_EVT_Card_Ready";
		app[app.subGameName + "Client"].OnEvent(key);
	},


	//---------------------获取接口------------------------------
	GetEnterRoomID: function () {
		return this.enterRoomID
	},

	//获取房间脚本
	GetEnterRoom: function () {
		if (!this.enterRoomID) {
			console.error("GetEnterRoom not enterRoom");
			return
		}
		return this.BJPKRoom
	},

	//-----------------------发包函数-----------------------------
	//发送创建房间
	SendCreateRoom: function (sendPack) {
		let msg = `${app.subGameName}.C${app.subGameName.toUpperCase()}CreateRoom`;
		this.NetManager.SendPack(msg, sendPack, null, function (error) {
			console.error("发送创建房间失败", error);
		});
		console.warn("发送创建房间",msg, sendPack);
	},

	//获取房间信息
	SendGetRoomInfo: function (roomID, callback) {
		console.log('==========LZBJPKRoomMgr SendGetRoomInfo==========')
		let msg = `${app.subGameName}.C${app.subGameName.toUpperCase()}GetRoomInfo`;
		this.NetManager.SendPack(msg, {"roomID": roomID}, callback, function (error) {
			console.error("获取房间信息错误", error);
		});
		console.warn("获取房间信息",msg, roomID, callback);
	},

	//开始游戏
	SendStartRoomGame: function (roomID) {
		let msg = "room.CBaseStartGame";
		this.NetManager.SendPack(msg, {"roomID": roomID});
		console.log("开始游戏",msg, roomID);
	},
	//发送看牌
	SendKanPai: function (roomID, pos) {
		let msg = `${app.subGameName}.C${app.subGameName.toUpperCase()}Kan`;
		this.NetManager.SendPack(msg, {"roomID": roomID, "pos": pos});
		console.log("发送看牌",msg, roomID, pos);
	},
	//发送位置执行指令 弃牌
	SendPosAction: function () {
		if (!this.enterRoomID) {
			console.error("SendPosAction not enterRoomID");
			return
		}
		let roomID = this.enterRoomID;

		let roomSet = this.GetEnterRoom().GetRoomSet();
		if (!roomSet) {
			console.error("SendPosAction not find roomSet");
			return
		}
		let setID = roomSet.GetRoomSetProperty("setID");
		/*let setRound = roomSet.GetRoomSetProperty("setRound");
		if (!setRound) {
			console.error("SendPosAction not find setRound");
			// return;
		}*/
		// let roundID = setRound["waitID"];
		let sendPack = {
			"roomID": roomID,
			"setID": setID,
			"abandonFlag": true,
		};
		console.log("发送弃牌data", sendPack);
		let msg = `${app.subGameName}.C${app.subGameName.toUpperCase()}Abandon`;
		this.NetManager.SendPack(msg, sendPack, null, function (error) {
			console.log("发送弃牌data失败", error);
		});
	},
	SendSitDown: function (roomID, pos) {
		let msg = `${app.subGameName}.C${app.subGameName.toUpperCase()}SitDown`;
		this.NetManager.SendPack(msg, {"roomID": roomID, "pos": pos});
		console.log("玩家换位置 ",msg, roomID, pos);

	},
	//玩家动作
	SendOpCard: function (sendPack) {
		if (!this.enterRoomID) {
			console.error("SendPosAction not enterRoomID");
			return
		}
		let roomID = this.enterRoomID;
		let roomSet = this.GetEnterRoom().GetRoomSet();
		if (!roomSet) {
			console.error("SendPosAction not find roomSet");
			return
		}
		let setID = roomSet.GetRoomSetProperty("setID");
		let setRound = roomSet.GetRoomSetProperty("setRound");
		if (!setRound) {
			console.error("SendPosAction not find setRound");
			return
		}
		let roundID = setRound["waitID"];
		sendPack['roomID'] = roomID;
		sendPack['setID'] = setID;
		sendPack['roundID'] = roundID;
		let msg = `${app.subGameName}.C${app.subGameName.toUpperCase()}OpCard`;
		this.NetManager.SendPack(msg, sendPack);
		console.log("玩家动作",msg, sendPack);
	},
	//发送等待继续游戏时间
	SendTimeOutContinue: function () {
		this.NetManager.SendPack(app.subGameName+".C"+app.subGameName.toUpperCase()+"TimeOutContinue", {"roomID": this.enterRoomID});
	},
});


var g_BJPKRoomMgr = null;

/**
 * 绑定模块外部方法
 */
exports.GetModel = function () {
	if (!g_BJPKRoomMgr)
		g_BJPKRoomMgr = new BJPKRoomMgr();
	return g_BJPKRoomMgr;

};
